﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UPMS.Core.Domain
{
    public class UpmsInitializer : DropCreateDatabaseIfModelChanges<UpmsContext>
    {
        protected override void Seed(UpmsContext context)
        {
            var hosts = new List<Host> {
                new Host{ Id=Guid.NewGuid(), IP="11.22.22.22", Name="host_01", Description="description"},
                new Host{ Id=Guid.NewGuid(), IP="11.22.22.22", Name="host_02", Description="description"}
            };
            hosts.ForEach(h => context.Hosts.Add(h));
            context.SaveChanges();

            var apps = new List<Application> { 
                new Application{ Id=Guid.NewGuid(), Name="app01", Host=hosts.FirstOrDefault()},
                new Application{ Id=Guid.NewGuid(), Name="app02", Host=hosts.FirstOrDefault()}
            };

            apps.ForEach(a => context.Applications.Add(a));

            context.SaveChanges();


            var modules = new List<Module> { 
                new Module{Id =Guid.NewGuid(), Name="app01.module01",Application=apps.FirstOrDefault()},
                new Module{Id =Guid.NewGuid(), Name="app01.module02",Application=apps.FirstOrDefault()}
            };

            modules.ForEach(a => context.Modules.Add(a));
            context.SaveChanges();

            var permissions = new List<Permission> { 
                new Permission{Id =Guid.NewGuid(), Name="app01.module01.delete",Module=modules.FirstOrDefault()},
                new Permission{Id =Guid.NewGuid(), Name="app01.module01.print",Module=modules.FirstOrDefault()}
            };

            permissions.ForEach(a => context.Permissions.Add(a));
            context.SaveChanges();


            var orgs = new List<Organization> { 
                new Organization{Id =Guid.NewGuid(), Name="org01"},
                new Organization{Id =Guid.NewGuid(), Name="org02"}
            };

            orgs.ForEach(a => context.Organizations.Add(a));
            context.SaveChanges();


            var departs = new List<Department> { 
                new Department{Id =Guid.NewGuid(), Name="org01.department01",Orgaization=orgs.FirstOrDefault()},
                new Department{Id =Guid.NewGuid(), Name="org01.department02",Orgaization=orgs.FirstOrDefault()}
            };

            departs.Add(new Department { Id = Guid.NewGuid(), Name = "org01.department01.02", Orgaization = orgs.FirstOrDefault(), ParentDepartment = departs.FirstOrDefault() });

            departs.ForEach(a => context.Departments.Add(a));
            context.SaveChanges();

            var groups = new List<Group> { 
                new Group{Id=Guid.NewGuid(),Name="app01.group01",Application=apps.FirstOrDefault()},
                new Group{Id=Guid.NewGuid(),Name="app01.group02",Application=apps.FirstOrDefault()}
            };

            groups.ForEach(a => context.Groups.Add(a));
            context.SaveChanges();

            var roles = new List<Role> { 
                new Role{Id=Guid.NewGuid(),Name="app01.group01.role01",Group=groups.FirstOrDefault(),Permissions=permissions},
                new Role{Id=Guid.NewGuid(),Name="app01.role01",Application=apps.FirstOrDefault(),Permissions=permissions}
            };

            roles.ForEach(a => context.Roles.Add(a));
            context.SaveChanges();

            var accounts = new List<Account> {
                new Account{Id=Guid.NewGuid(), Name="zlddtt", Groups=groups, Status=1, Password="password", Application=apps.FirstOrDefault() },
                new Account{Id=Guid.NewGuid(), Name="zlddtt2", Roles=roles,  Status=2, Password="password", Application=apps.FirstOrDefault() }
            };

            accounts.ForEach(a => context.Accounts.Add(a));
            context.SaveChanges();

            var users = new List<User> { 
                new User{Id=Guid.NewGuid(),UserName="43789540", UserType=1, Accounts=accounts},
                new User{Id=Guid.NewGuid(),UserName="zlddtt@hotmail.com",UserType=2, Accounts=accounts}
            };

            users.ForEach(a => context.Users.Add(a));
            context.SaveChanges();
        }
    }
}
